package com.hundsun.cprs.yyt.service.match.core.queue;

import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import com.hundsun.cprs.yyt.service.match.core.queue.visitor.QueueVisitor;
import com.hundsun.cprs.yyt.service.match.domain.TradeEntrust;

public class PriceCell {

    private long price;

    private PriceCell next;

    private EntrustQueue entrustQueue = new EntrustQueue();

    private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

    private ReadLock readLock = lock.readLock();

    private WriteLock writeLock = lock.writeLock();

    public PriceCell(long price) {
        super();
        this.price = price;
    }

    public void accept(QueueVisitor visitor) {
        visitor.visit(this);
    }

    /**
     * 订单队列是否为空
     * 
     * @return
     */
    public boolean isEntrustEmpty() {
        return this.getEntrustSum() == 0;
    }

    /**
     * 得到订单队列长度(有多少个订单)
     */
    public int getEntrustSum() {
        return this.entrustQueue.getEntrustSum();
    }

    /**
     * 得到次价格档位中剩余总的份额委托数量
     * 
     * @return
     */
    public long getSurplusEntrustAmount() {
        return this.getEntrustQueue().getSurplusEntrustAmount();
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append("price", price)
            .append("entrustQueue", entrustQueue).toString();
    }

    public void addEntrust(TradeEntrust entrust) {
        this.entrustQueue.addEntrust(entrust);
    }

    public long getPrice() {
        return price;
    }

    public PriceCell getNext() {
        return next;

    }

    public void setNext(PriceCell next) {
        this.next = next;
    }

    public WriteLock getWriteLock() {
        return writeLock;
    }

    public ReadLock getReadLock() {
        return readLock;
    }

    public EntrustQueue getEntrustQueue() {
        return entrustQueue;
    }

    public boolean removeEntrust(TradeEntrust entrust) {
        if (entrust == null) {
            return false;
        }
        return this.entrustQueue.removeEntrust(entrust);
    }

}
